package us.lithub.ui.dialog;

import us.lithub.R;
import us.lithub.data.Book;
import us.lithub.data.ItemLookup;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;

/**
 * The Class PreviewAddedBook shows a preview of the book before it is added to the library
 */
public class PreviewAddedBook extends Activity {

	private Intent data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_preview_added_book);

		// Get the book information via the upc passed in from the intent
		data = getIntent();
		String upc = data.getStringExtra(Book.UPC);
		Book book = ItemLookup.findProduct(upc);
		data.putExtra(Book.ISBN, book.getIsbn());
		
		// Set the text of the various view components
		((TextView) findViewById(R.id.Title)).setText(book.getTitle());
		((TextView) findViewById(R.id.Author)).setText(book.getAuthor());
		((TextView) findViewById(R.id.Publisher)).setText(book.getPublisher());
		((TextView) findViewById(R.id.Summary)).setText(book.getSummary());
		
		// Lazy load the cover image
		ImageView image = (ImageView)findViewById(R.id.imview);
		new ImageLoader(this).DisplayImage("http://covers.openlibrary.org/b/isbn/"+upc+"-M.jpg", image);
	}

	/**
	 * Invoked when the user presses the okay button
	 */
	public void addBook(View v) {
		setResult(RESULT_OK, data);
		finish();
	}

	/**
	 * Invoked when the user presses the cancel button
	 */
	public void cancel(View v) {
		finish();
	}
}
